<template>
  <div>
    <el-card class="box-card">
      <div slot="header" style="text-align:left">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="商品">
            <el-input v-model="searchForm.productName" placeholder="商品名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
           <el-form-item>
            <el-button type="primary" @click="backList">返回</el-button>
          </el-form-item>
           <el-form-item>
            <el-button type="primary" @click="add">添加</el-button>
          </el-form-item>
        </el-form>
       
      </div>
      <div style="color:red" v-if="warnNum>0">
          当前价格警告数：{{warnNum}}
      </div>
      <el-table :data="products" border style="width: 100%">
        <el-table-column prop="productImage" label="图片" style="width: 20%">
              <template slot-scope="scope">
                  <img width=50 height=50 v-if="scope.row.productImage && scope.row.productImage.length>0" :src="baseImage+scope.row.productImage">
                  <span v-else>无图</span>
              </template>
          </el-table-column>
        <el-table-column prop="productName" label="商品名" style="width: 20%"></el-table-column>
           <el-table-column prop="amount" label="数量" style="width: 50%"></el-table-column>
         <el-table-column prop="salePrice" label="原销售价" style="width: 20%"></el-table-column>
         <el-table-column prop="reduction" label="减免" style="width: 50%"></el-table-column>
         <el-table-column prop="reductionPrice" label="减免后价" >
            <template slot-scope="scope">
                 <span v-if="scope.row.reductionPrice==0.01" style="background:yellow"> {{scope.row.reductionPrice}} 
                      <span style="color:red">警告</span>
                   </span>
                   <span v-else>
                     {{scope.row.reductionPrice}}
                   </span>
              </template>
         </el-table-column>
        <el-table-column label="操作" style="width:10%">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showChangePrice(scope.row)">改价</el-button>        
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top:10px;">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page.sync="pagenow"
          :page-size="pagesize"
          layout="total, prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>

     <el-dialog :visible.sync="dialog"  width="40%" center style="text-align: center" >
          <el-transfer :titles="['源商品', '选中商品']" v-model="chooseProducts" :data="transData"  @change="handleChange" ></el-transfer>
    </el-dialog>
   
    <el-dialog :visible.sync="priceDialog"  width="40%" center style="text-align: center" >
          <el-form   label-width="80px" > 
           <el-form-item label="减免" >
               <el-input-number   v-model="priceForm.reduction" :precision="2" :min="0" :step="1" ></el-input-number>
           </el-form-item>
           <el-form-item label="数量" >
               <el-input-number   v-model="priceForm.amount"  :precision="0"  :min="1" :step="1" ></el-input-number>
           </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="changePrice">更新</el-button>
            <el-button @click="priceDialog=false">取消</el-button>
          </el-form-item>
        </el-form>
    </el-dialog>
     
  </div>
</template>

<script>
import { timeperiodProductList ,productSearch,timeperiodProductBind,timeperiodProductPrice} from "@/libs/api.js";
export default {
  methods: {
    backList(){
      this.$router.push({
        path: "/seckill/timeperiod/list?id="+this.$route.query.sid
      });
    },
    showChangePrice(row){
      this.priceForm.id=row.id
      this.priceForm.reduction=row.reduction;
      this.priceForm.reductionPrice=row.reductionPrice;
      this.priceForm.amount=row.amount;
      this.priceDialog=true;
    },

    /**选中商品变化 */
    handleChange(value, direction, movedKeys) {
        this.warnNum=0;
         var param ={"tid":this.$route.query.id,"sid":this.$route.query.sid};
         let products =[];
         if(this.transData && this.transData.length>0){
            this.transData.forEach((item, index) => {
              if(item.reductionPrice==0.01){
                this.warnNum++;
              }
              for(var i=0;i<value.length;i++){
                  if (item.id == value[i]) {
                    products.push(this.transData[index]);
                    break;
                  }
              }
            
            })
         }
         param.products=products;
        timeperiodProductBind(param).then(res => {
          this.pagenow = 1;
          this.loadPage();
        });
    },
    /**
     * 分页
     */
    handleCurrentChange(p) {
      this.loadPage(p);
    },
    //改价
    changePrice(){
      timeperiodProductPrice(this.priceForm).then(res => {
        this.priceDialog=false;
        if(this.products && this.products.length>0){
          for(var i=0;i<this.products.length;i++){
             if(this.products[i].id==this.priceForm.id){
                this.products[i].reduction=res.reduction;
                this.products[i].reductionPrice=res.reductionPrice;
                 this.products[i].amount=res.amount;
                if(this.priceForm.reductionPrice==0.01 && res.reductionPrice>0.01){
                    this.warnNum--;
                }
                if(res.reductionPrice==0.01){
                    this.warnNum++;
                }
                break;
             }
          }
        }
      });
    },
    onSubmit() {
      this.pagenow = 1;
      this.loadPage();
    },
    loadPage() {
      var param = { pagesize: 10, pagenow: this.pagenow,"timePeriodId":this.$route.query.id,"name":this.searchForm.productName};
      timeperiodProductList(param).then(res => {
        this.products = res.rows;
        this.total = res.total;
        this.warnNum=0;
        if(this.products && this.products.length>0){
          for(var i=0;i<this.products.length;i++){
             if(this.products[i].reductionPrice==0.01){
               this.warnNum++;
             }
          }
        }
      });
    },
    add(){
     this.dialog=true;
    },
  },
  mounted() {
    
    this.loadPage();
    var param ={"pagenow":"0"};
    productSearch(param).then(res => {
      let products = res.rows;
      if(products && products.length>0){
          this.transData=[];
          for(var i=0;i<products.length;i++){
            products[i].key=products[i].id;
            products[i].label=products[i].name;
            delete products[i].detailpc;
            delete products[i].detailh5;
            this.transData.push(products[i]);
          }
      }
    });
     this.baseImage=this.$http.baseImage;
  },

  data() {
    return {
      dialog:false,
      priceDialog:false,
      products: [],
      transData:[],
      priceForm:{id:"",reduction:0,reductionPrice:0.01,amount:1},
      chooseProducts:[],
      total: 0,
      pagenow: 1,
      pagesize: 10,
      baseImage:"",
      warnNum:0,
      searchForm: {
        productName: ""
      }
    };
  }
};
</script>